//
//  Player.m
//  AudioQueueDemo
//
//  Created by marco on 16/3/8.
//  Copyright © 2016年 Marco. All rights reserved.
//

#import "Player.h"
#import "AQQueue.h"

@implementation Player


-(void)startQueue{
    AudioQueueStart(audioQueue, NULL);
    
    for(int i=0;i<QUEUE_BUFFER_SIZE;i++)
    {
        AudioQueueAllocateBuffer(audioQueue, MIN_SIZE_PER_FRAME, &audioQueueBuffers[i]);
        [self readPCMAndPlay:audioQueue buffer:audioQueueBuffers[i]];
    }
}
- (id) init{
    self = [super init];
    if (self)
    {
        [self initAudio];
    }
    return self;
}

-(void)initAudio
{
    ///设置音频参数
    audioDescription.mSampleRate = 16000;//采样率
    audioDescription.mFormatID = kAudioFormatLinearPCM;
    audioDescription.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
    audioDescription.mChannelsPerFrame = 1;///单声道
    audioDescription.mFramesPerPacket = 1;//每一个packet一侦数据
    audioDescription.mBitsPerChannel = 16;//每个采样点16bit量化
    audioDescription.mBytesPerFrame = 2;
    audioDescription.mBytesPerPacket = 2 ;
    
    AudioQueueNewOutput(&audioDescription, AudioPlayerAQInputCallback, (__bridge void * _Nullable)(self), nil, nil, 0, &audioQueue);//使用player的内部线程播
    
    _flag = TRUE;
    
}
-(void)readPCMAndPlay:(AudioQueueRef)outQ buffer:(AudioQueueBufferRef)outQB{
    
    @synchronized(self.voiceDataQueue){
        if (self.voiceDataQueue.count <=0 && _flag) {
            AudioQueueEnqueueBuffer(outQ, outQB, 0, NULL);
        }else if(self.voiceDataQueue.count > 0 && _flag){
            NSData *myData  = [self.voiceDataQueue dequeue];
            Byte *pcmDataByte = (Byte *)myData.bytes;
            outQB->mAudioDataByteSize = (UInt32)myData.length;
            Byte *audiodata = (Byte *)outQB->mAudioData;
            for(int i=0;i<myData.length;i++)
            {
                audiodata[i] = pcmDataByte[i];
            }
            AudioQueueEnqueueBuffer(outQ, outQB, 0, NULL);
        }
    }
    
    
}
static void AudioPlayerAQInputCallback(void *input, AudioQueueRef outQ, AudioQueueBufferRef outQB)
{
    Player *mainviewcontroller = (__bridge Player *)input;
    [mainviewcontroller readPCMAndPlay:outQ buffer:outQB];
    
}

@end
